<?php

namespace App\Http\Controllers\Admin;

use App\Craftsman;
use App\Exceptions\CreateException;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/**
 * @property Craftsman model
 */
class CraftsmanController extends Controller
{
    public function __construct(Craftsman $model)
    {
        $this->model = $model;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $page  = $request->get('page',1);
        $size  = $request->get('size',10);
        return $this->page($this->model->limit($size)->offset(($page-1)*$size)->with('teams')->with('header:id,phone')->get(),$this->model->count());
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws CreateException
     */
    public function store(Request $request)
    {
        //表数据
        $data = $request->only(['name','level','mens','team_id','lng','lat','project_id','images']);
        $header = $request->only(['city','hope_city','name','lng','lat','card','phone','headman_images']);
        if ($res = (new \App\Logic\Craftsman())->insertCraftsman($data,$header))
            return $this->success();
        throw new CreateException();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\JsonResponse
     * @throws CreateException
     */
    public function update(Request $request, $id)
    {

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
